package com.rams.service.impl;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rams.dao.LoginDao;
import com.rams.dao.UserDao;
import com.rams.domain.vo.UserExpenditure;
import com.rams.domain.vo.ItemVO;
import com.rams.domain.vo.User;
import com.rams.domain.vo.UserCircle;
import com.rams.service.UserService;
import com.rams.service.vo.DisplayVO;
import com.rams.service.vo.UserReportVO;

/**
 * @author Swapnil Desai
 *
 */
@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	UserDao userDao;
	
	@Autowired
	LoginDao loginDao;
	
	@Override
	public DisplayVO addNewCircle(UserCircle circle, String userName) {
		Random random = new SecureRandom();
		DisplayVO dispVO = new DisplayVO();
		circle.setCircleID(String.valueOf(random.nextInt()));
		circle.setMembers(Arrays.asList(circle.getMembers().get(0).split(",")));
		User user = userDao.addNewCircle(circle, userName);
		dispVO.setUserCircles(userDao.getUserCircles(user));
		dispVO.setUser(user);
		dispVO.setStatus(true);
		return dispVO;
	}

	@Override
	public DisplayVO getCircleData(String circleId, String userId) {
		DisplayVO displayVO = new DisplayVO();
		if(null != circleId && !circleId.equals("")){
			displayVO.setUserCircle(userDao.getCircle(circleId));
			displayVO.setUser(loginDao.getUser(userId));
			List<UserExpenditure> data = userDao.getCircleAmountData(circleId);
			displayVO.setCircleExpenditurePerUser(data);
		}
		
		return displayVO;
	}
	
	@Override
	public ItemVO getItemData(String circleId, ItemVO itemvo) {
		
		if(null != circleId && !circleId.equals("")){
			UserCircle circle = userDao.getCircle(circleId);
			itemvo.setCircleID(circle.getCircleID());
			itemvo.setCircleName(circle.getCircleName());
			
		}
		
		return itemvo;
	}
	
	@Override
	public ItemVO addItemData(ItemVO itemvo) {
		
		itemvo.setItems(Arrays.asList(itemvo.getItems().get(0).split(",")));
		userDao.addItem(itemvo);
		return itemvo;
	}
	
	@Override
	public UserReportVO getUserItemsReport(String circleId, String username) {
		UserReportVO userReport = new UserReportVO();
		userReport.setItems(userDao.getuserItemReportData(circleId, username));
		return userReport;
	}
}
